/**
 * Created by gaoyj on 2018/6/28.
 */
window.onload = function (ev) {
    topbar.hide();
};
$("#xianyuPrice_id").hide();
$("#xianyuPrice").hide();
initProvinces();
$("#quickSetting").validate({
    rules: {
        minPrice: {
            required: true,
            range: [0.5, 1.0]
        }, maxPrice: {
            required: true,
            range: [0.5, 1.1]
        }, xianyuPrice: {
            required: true,
            range: [0, 1.0]
        }

    }
});
$.ajax({
        type: "POST",
        url: "/CardProductController/queryAllCardOperatorList",
        dataType: "json",
        success: function (data) {
            $("#productType").html("");
            var str = "<option value=\"\">全部</option>";
            if (data != null && data.length > 0) {
                for (var i = 0; i < data.length; i++) {
                    str += '<option value=' + data[i].cardOperatorId + '>' + data[i].cardOperatorName + '</option>'
                }
            }
            $("#productType").append(str);
            //缺一不可  
            $('.selectpicker').selectpicker('refresh');
            $('.selectpicker').selectpicker('render');
        },
        beforeSend: function () {
        }
    });
//会员密价克隆
$("#cloneId").click(function () {
    var productType = $("#productType").val();
    if (productType == null || productType == undefined){
        layer.msg("请选择运营商");
        return;
    }
    $.confirm({
        title: '确认!',
        content: '确定要执行克隆操作吗？',
        confirm: function () {
            $.ajax({
                type: "POST",
                url: "/productPrice/cloneUserPrice",
                data: {
                    productType: $("#productType").val(),
                    sourceUserAccount: $("#sourceUserAccount").val(),
                    targetUserAccount: $("#targetUserAccount").val(),
                },
                dataType: "json",
                success: function (data) {
                    if (data.success) {
                        layer.msg(data.message);
                    } else {
                        layer.msg(data.message);
                    }
                }
            })
        }
    })
});
function initProvinces() {
    var str = '';
    for (var i = 0; i < zone.length; i++) {
        str += '<label class="icheck" style="margin-right:10px;">' +
            '<input name="areas" type="checkbox" value="' + zone[i] + '">' + zone[i] + '</label>';
    }
    $("#areaSet").html(str);
}

$("#searchUsers").click(function () {
    var xianyuUserAccount = '18615327155';
    var userAccount = $.trim($("#userAccount").val());
    if (userAccount.indexOf(xianyuUserAccount) != -1 && userAccount.indexOf(",") != -1) {
        layer.msg("仅支持单独设置闲鱼会员密价");
        return;
    }
    $.post("/productPrice/queryUserByAccount", {userAccount: $.trim($("#userAccount").val())}, function (data) {
        //隐藏闲鱼价格
        if (userAccount.indexOf(xianyuUserAccount) != -1) {
            $("#xianyuPrice_id").show();
            $("#xianyuPrice").show();
        } else {
            $("#xianyuPrice_id").hide();
            $("#xianyuPrice").hide();
        }
        if (data == null || data.length == 0) {
            layer.msg("请输入正确会员编号");
            return;
        }
        var str = '';
        for (var i = 0; i < data.length; i++) {
            str += '<label class="icheck" style="margin-right:10px;">' +
                '<input name="userIds" type="checkbox" value="' + data[i].userId + '">' + data[i].supInfo.name + '</label>';
        }
        $("#userSet").html(str);

    });
})

function reloadDiscount(obj) {
    if ($(obj).val() == '1' || $(obj).val() == '0') {
        $("#minPrice").removeAttr("readonly", "readonly")
        $("#maxPrice").removeAttr("readonly", "readonly")
    } else {
        $("#minPrice").attr("readonly", "readonly");
        $("#maxPrice").attr("readonly", "readonly");
    }
}

$("#formSubmit").click(function () {
    if ($('input[name=userIds]:checked').length < 1) {
        layer.msg("请选择会员");
        return;
    }
    if ($('input[name=productTypes]:checked').length < 1) {
        layer.msg("请选择运营商");
        return;
    }
    if ($('input[name=faces]:checked').length < 1) {
        layer.msg("请选择面值");
        return;
    }
    if ($('input[name=areas]:checked').length < 1) {
        layer.msg("请选择销卡地区");
        return;
    }
    if ($("#method").val() == 0 || $("#method").val() == 1) {
        var v = $("#quickSetting").valid();
        if (!v) {
            return;
        }
        if ($("#minPrice").val() > $("#maxPrice").val()) {
            layer.msg("最大折扣不可小于最低折扣");
            return;
        }
    }
    $.confirm({
        title: '确认!',
        content: '确定提交吗？',
        confirm: function () {
            submitForm();
        }
    })
});
$('#cloneUserPriceForm').form({
    onSubmit: function () {
        return confirm('是否确定要执行克隆操作?');
    },
    success: function (data) {
        try {
            data = eval("(" + data + ")");
        } catch (e) {
        }
        layer.msg(data.message);
    }
});

function submitForm() {
    $.post("/productPrice/setUpUserPrice", $("#quickSetting").serialize(), function (data) {
        layer.msg(data.message);
    }).error(function (e) {
        layer.msg(e);
    });
}
